package training.sample.app.web.converters;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.ParseException;
import org.springframework.format.Formatter;
import org.springframework.stereotype.Component;

import training.sample.app.domain.Country;
import training.sample.app.service.CountryService;

@Component
public class CountryFormatter implements Formatter<Country> {
	
	@Autowired
	private CountryService countryService;
     
    @Override
    public Country parse(String id, Locale locale) throws ParseException {
    	Country country = null;
    	
    	if (!"NONE".equals(id)) {
    		country = countryService.getCountryByID(Long.parseLong(id));
    	} else {
    		country = new Country();
    	}
    	
        return country;
    }

    public String print(Country c, Locale locale) {
        return c.getName();
    };
}
